package com.assic.utils.db;

import java.util.Iterator;
import java.util.Map;

public class Sqls {
	/**
	 * 生成插入语句
	 * 
	 * @param data
	 * @param tableName
	 * @return
	 */
	public static String genInsertSql(Map data, String tableName) {
		// insert into table(keys) values(values);
		String target = "insert into " + tableName + "( ";
		String keys = "";
		String values = "";
		Iterator ite = data.keySet().iterator();
		while (ite.hasNext()) {
			String key = (String) ite.next();
			Object value = data.get(key);
			String valueInfo = "";
			if (value instanceof Number) {
				valueInfo = value.toString();
			} else {
				valueInfo = value == null ? "null" : "'" + value.toString() + "'";
			}
			keys += key + ", ";
			values += valueInfo + ", ";
		}
		target += keys.substring(0, keys.length() - 2);
		target += " ) values ( ";
		target += values.substring(0, values.length() - 2);
		return target + " )";
	}

}
